#include "usart1.h"

void USART1_GPIO_NVIC_Init()
{
    LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);

    // TX GPIO
    GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
    GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
    GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
    LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // RX GPIO
    GPIO_InitStruct.Pin = LL_GPIO_PIN_10;
    GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
    LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // 中断向量
    NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
    NVIC_EnableIRQ(USART1_IRQn);
}

void USART1_Init(void) /* USART1初始化函数 */
{
    LL_USART_InitTypeDef USART_InitStruct = {0};
    USART1_GPIO_NVIC_Init();
    // 串口参数
    USART_InitStruct.BaudRate = BAUDRATE;
    USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
    USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
    USART_InitStruct.Parity = LL_USART_PARITY_NONE;
    USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
    USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
    USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
    LL_USART_Init(USART1, &USART_InitStruct);
    LL_USART_ConfigAsyncMode(USART1);
    LL_USART_ClearFlag_RXNE(USART1);
    LL_USART_EnableIT_RXNE(USART1); // 收到非空中断
    LL_USART_ClearFlag_TC(USART1);
    LL_USART_EnableIT_TC(USART1); // 发送完中断
    LL_USART_Enable(USART1);
}

void USART1_Init_Without_Interrupt(uint32_t buad) /* USART1初始化函数,不用中断,手动处理数据 */
{
    LL_USART_InitTypeDef USART_InitStruct = {0};
    USART1_GPIO_NVIC_Init();
    // 串口参数
    USART_InitStruct.BaudRate = buad;
    USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
    USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
    USART_InitStruct.Parity = LL_USART_PARITY_NONE;
    USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
    USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
    USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
    LL_USART_Init(USART1, &USART_InitStruct);
    LL_USART_ConfigAsyncMode(USART1);
    LL_USART_ClearFlag_RXNE(USART1);
    LL_USART_ClearFlag_TC(USART1);
    LL_USART_Enable(USART1);
}

void USART1_IRQHandler(void) /* 数据发送和接收中断 */
{
    uint32_t tmp;
    if (LL_USART_IsActiveFlag_RXNE(USART1))
    {
        tmp = USART1->SR;
        (void)tmp;
        tmp = USART1->DR;
    }
    else if (LL_USART_IsActiveFlag_TC(USART1))
    {
        LL_USART_ClearFlag_TC(USART1);
    }
}

/*
    达到时间返回 0
 */

uint32_t USART1_read_data(uint8_t *buf, uint32_t size, uint32_t timeoutms)
{
    uint32_t count = 0;
    uint32_t n_ms = 0;
    LL_USART_ClearFlag_RXNE(USART1);
    // 收字节
    while (1)
    {
        add_1_per_1ms(&n_ms);
        if (n_ms > timeoutms)
        {
            break;
        }
        if (LL_USART_IsActiveFlag_RXNE(USART1))
        {
            *buf++ = USART1->DR;
            if (++count == size) // 命令字节达到数量返回
            {
                break;
            }
        }
    }
    return count;
}

void USART1_write_string(uint8_t *str)
{
    while (*str != 0x00)
    {
        USART1->DR = *str++;
        while (!LL_USART_IsActiveFlag_TXE(USART1))
        {
        }
    }
    while (!LL_USART_IsActiveFlag_TC(USART1))
    {
    }
}

void USART1_write_uint32(uint32_t n)
{
    uint8_t i = 4;
    while (i--)
    {
        USART1->DR = n;
        n >>= 8;
        while (!LL_USART_IsActiveFlag_TXE(USART1))
        {
        }
    }
    while (!LL_USART_IsActiveFlag_TC(USART1))
    {
    }
}

void USART1_print_HEX32(uint32_t n)
{
    const uint8_t tab[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    uint8_t i = 8;
    while (i--)
    {
        USART1->DR = tab[(n >> (4 * i)) & 0x0F];
        while (!LL_USART_IsActiveFlag_TXE(USART1))
        {
        }
    }
    while (!LL_USART_IsActiveFlag_TC(USART1))
    {
    }
}
